extends Node


@warning_ignore("unused_signal")
signal checkpoint_start

@warning_ignore("unused_signal")
signal checkpoint_complete(checkpoint: Checkpoint)

@warning_ignore("unused_signal")
signal stage_complete

@warning_ignore("unused_signal")
signal stage_interim

## 霸屏UI显示时触发
@warning_ignore("unused_signal")
signal screen_ui_display(screenUi: UI.Screen, display: bool)

@warning_ignore("unused_signal")
signal player_dead


func player_death():
	SoundPlayer.play_death()
	player_dead.emit()



func stage_pause(pause: bool, delay: float = 0.0):
	if delay < 0.0001:
		get_tree().paused = pause
		return
	await get_tree().create_timer(delay).timeout
	get_tree().paused = pause


# 玩家重生
func reborn_player():
	var player := get_tree().get_first_node_in_group(GlobalConstants.Group.PLAYER_GROUP) as Player
	if not player:
		return
	player.visible = false

	var camera := get_viewport().get_camera_2d()
	player.global_position = camera.position + player.reborn_position_offset
	player.spawn_entry_mode = Character.EntryMode.DROP
	player.spawn_drop_height = 50.0
	player.set_current_health(player.max_health)
	player.set_reborn_node_state()
	player.visible = true




# 创建检查点中的敌人，注意，创建完成后不要挂载敌人节点，是为了防止挂载的父级存在偏移量导致敌人节点的全局坐标额外增加了偏移量。
# 使用 orphan_actor内部会判断节点是否已经挂载，会自动挂载
func spawn_enemy_in_checkpoint(enemy_data: EnemyData, checkpoint: Checkpoint):
	var character := _create_enemy(enemy_data)
	if not character:
		return
	character.death.connect(checkpoint.on_enemy_death)
	EntityManager.orphan_actor.emit(character)


func spawn_enemy_in_door(enemy_data: EnemyData, door: Door):
	var character := _create_enemy(enemy_data)
	if not character:
		return
	character.death.connect(door.on_enemy_death)
	EntityManager.orphan_actor.emit(character)


func _create_enemy(enemy_data: EnemyData) -> BasicEnemy:
	if not ActorsContainer.PREFAB_ENEMY_MAP.has(enemy_data.type):
		print("Invalid enemy type or data is empty")
		return null
	var character := ActorsContainer.PREFAB_ENEMY_MAP[enemy_data.type].instantiate() as BasicEnemy
	character.global_position = enemy_data.origin_point
	character.spawn_entry_mode = enemy_data.entryMode
	character.spawn_drop_height = enemy_data.dropHeight
	character.spawn_appear_time = enemy_data.appearTime
	character.enemy_type = enemy_data.type
	return character
